package com.example.demo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBUtil {

    //这里可以设置数据库名称&useSSL=false
    private final static String URL = "jdbc:sqlserver://106.15.33.108:1433;DatabaseName=CurriculumDesignDB";
    private static final String USER="sa";
    private static final String PASSWORD="123456aA";

    private static Connection conn=null;
    //静态代码块（将加载驱动、连接数据库放入静态块中）
    static{
        try {
            //1.加载驱动程序
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            //2.获得数据库的连接
            conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //对外提供一个方法来获取数据库连接
    public static Connection getConnection(){
        return conn;
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        DBUtil test=new DBUtil();

        try {
            test.getConnection();
            System.out.println("数据库链接成功！");
        }catch(Exception e) {
            e.printStackTrace();
        }}
}
